<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/12/13
 * Time: 10:17
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Goods extends Base
{
    /**
     * 商品展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $goods_name = input('goods_name'); // 商品名
            $goods_f_id = input('goods_f_id'); // 一级分类
            $goods_s_id = input('goods_s_id'); // 二级分类
            $goods_status = input('goods_status'); // 状态
            $goods_code = input('goods_code'); // 货号
            $where['goods_del'] = 1;
    
            if(isset($goods_name) && $goods_name != ''){
                $where['goods_name'] = ['like', '%' . $goods_name . '%'];
            }
            if(isset($goods_status) && $goods_status != ""){
                $where['goods_status'] = $goods_status;
            }
            if(isset($goods_code) && $goods_code != ''){
                $where['goods_code'] = ['like', '%' . $goods_code . '%'];
            }
            if(isset($goods_f_id) && $goods_f_id != '' && isset($goods_s_id) && $goods_s_id == ''){
                $where['goods_f_id'] = $goods_f_id;
            }
            if(isset($goods_f_id) && $goods_f_id != '' && isset($goods_s_id) && $goods_s_id != ''){
                $where['goods_f_id'] = $goods_f_id;
                $where['goods_s_id'] = $goods_s_id;
            }
        
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('goods')
                ->join('up_cate uc1', 'uc1.cate_id = goods_f_id', 'left')
                ->join('up_cate uc2', 'uc2.cate_id = goods_s_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('goods')
                ->join('up_cate uc1', 'uc1.cate_id = goods_f_id', 'left')
                ->join('up_cate uc2', 'uc2.cate_id = goods_s_id', 'left')
                ->where($where)
                ->field('goods_id, goods_name, goods_thumb, uc1.cate_name f_name, uc2.cate_name s_name, 
                goods_is_hot, goods_time, goods_stock, goods_code, goods_status')
                ->order('goods_time desc')
                ->page($NowPage, $limits)
                ->select();
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $f_cate = Db::table('up_cate')
            ->field('cate_id, cate_name')
            ->where('cate_level', 1)
            ->where('cate_status', 1)
            ->order('cate_sort asc')
            ->select();
        $this->assign([
            'f_cate' => $f_cate
        ]);
        return $this->fetch();
    }
    
    /**
     * 添加商品
     * @return mixed|\think\response\Json
     */
    public function goodsAdd(){
        if(Request::instance()->isPost()){
            $param = input('');
            if(empty($param['goods_f_id'])){
                return json(['code' => 100, 'msg' => '请选择一级分类']);
            }
            if(empty($param['goods_s_id'])){
                return json(['code' => 100, 'msg' => '请选择二级分类']);
            }
            if(empty($param['photo'])){
                return json(['code' => 100, 'msg' => '请上传主图']);
            }
            if(empty($param['goods_thumb'])){
                return json(['code' => 100, 'msg' => '请上传缩略图']);
            }
            $insert = [
                'goods_name' => trim($param['goods_name']),
                'goods_thumb' => $param['goods_thumb'],
                'goods_images' => $param['photo'],
                'goods_f_id' => $param['goods_f_id'],
                'goods_s_id' => $param['goods_s_id'],
                'goods_is_hot' => $param['goods_is_hot'],
                'goods_stock' => $param['goods_stock'],
                'goods_intro' => $param['goods_intro'],
                'goods_serve' => $param['goods_serve'],
                'goods_price' => $param['goods_price'],
                'goods_freight' => $param['goods_freight'],
                'goods_time' => date('Y-m-d H:i:s'),
                'goods_update_time' => date('Y-m-d H:i:s'),
                'goods_code' => $param['goods_code']
            ];
            $res = Db::table('up_goods')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        // 分类
        $f_cate = Db::table('up_cate')
            ->field('cate_id, cate_name')
            ->where('cate_level', 1)
            ->where('cate_status', 1)
            ->order('cate_sort asc')
            ->select();
        // 运费模版
        $fre = Db::table('up_fre')->where('fre_status', 1)
            ->field('fre_id, fre_name')
            ->order('fre_time desc')
            ->select();
        $this->assign([
            'f_cate' => $f_cate,
            'fre' => $fre
        ]);
        return $this->fetch('goods/goodsadd');
    }
    
    /**
     * 商品详情
     * @return mixed
     */
    public function goodsDetail(){
        $goods_id = input('goods_id');
        $detail = Db::table('up_goods')
            ->join('up_cate uc1', 'uc1.cate_id = goods_f_id', 'left')
            ->join('up_cate uc2', 'uc2.cate_id = goods_s_id', 'left')
            ->field('goods_name, goods_thumb, goods_images, uc1.cate_name f_name, uc2.cate_name s_name, 
            goods_is_hot, goods_time, goods_stock, goods_intro, goods_serve, goods_code, goods_price, 
            goods_id')
            ->where('goods_id', $goods_id)
            ->find();
        $detail['goods_images'] = explode(',', trim($detail['goods_images'], ','));
        $this->assign('goods', $detail);
        return $this->fetch('goodsdetail');
    }
    
    /**
     * 编辑商品
     * @return mixed|\think\response\Json
     */
    public function goodsEdit(){
        $goods_id = input('goods_id');
        if(Request::instance()->isPost()){
            $param = input('');
            if(empty($param['goods_f_id'])){
                return json(['code' => 100, 'msg' => '请选择一级分类']);
            }
            if(empty($param['goods_s_id'])){
                return json(['code' => 100, 'msg' => '请选择二级分类']);
            }
            if(empty($param['photo'])){
                return json(['code' => 100, 'msg' => '请上传主图']);
            }
            $update = [
                'goods_name' => trim($param['goods_name']),
                'goods_thumb' => $param['img1'],
                'goods_images' => $param['photo'],
                'goods_f_id' => $param['goods_f_id'],
                'goods_s_id' => $param['goods_s_id'],
                'goods_is_hot' => $param['goods_is_hot'],
                'goods_stock' => $param['goods_stock'],
                'goods_intro' => $param['goods_intro'],
                'goods_update_time' => date('Y-m-d H:i:s'),
                'goods_code' => $param['goods_code'],
                'goods_price' => $param['goods_price'],
                'goods_serve' => $param['goods_serve'],
                'goods_freight' => $param['goods_freight']
            ];
            $res = Db::table('up_goods')->where('goods_id', $param['goods_id'])->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        // 详情
        $detail = Db::table('up_goods')
            ->field('goods_name, goods_thumb, goods_images, goods_is_hot, goods_stock, goods_intro, 
            goods_code, goods_f_id, goods_s_id, goods_price, goods_serve, goods_freight')
            ->where('goods_id', $goods_id)
            ->find();
        // 一级分类
        $f_cate = Db::table('up_cate')
            ->field('cate_id, cate_name')
            ->where('cate_level', 1)
            ->where('cate_status', 1)
            ->order('cate_sort asc')
            ->select();
        // 二级分类
        $s_cate = Db::table('up_cate')
            ->field('cate_id, cate_name')
            ->where('cate_level', 2)
            ->where('cate_status', 1)
            ->where('cate_fid', $detail['goods_f_id'])
            ->order('cate_sort asc')
            ->select();
        // 运费模版
        $fre = Db::table('up_fre')->where('fre_status', 1)
            ->field('fre_id, fre_name')
            ->order('fre_time desc')
            ->select();
        $this->assign([
            'goods' => $detail,
            'f_cate' => $f_cate,
            's_cate' => $s_cate,
            'goods_id' => $goods_id,
            'fre' => $fre
        ]);
        return $this->fetch('goodsedit');
    }
    
    /**
     * 商品状态
     * @return \think\response\Json
     */
    public function goodsStatus(){
        $goods_id = input('id');
        $status = Db::table('up_goods')->where('goods_id', $goods_id)->value('goods_status');
        if($status == 1){
            $goods_status = 2;
        }else{
            $goods_status = 1;
        }
        $res = Db::table('up_goods')->where('goods_id', $goods_id)->setField('goods_status', $goods_status);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' => '修改失败']);
        }
    }
    
    /**
     * 删除商品
     * @return \think\response\Json
     */
    public function goodsDel(){
        $goods_id = input('id');
        $res = Db::table('up_goods')->where('goods_id', $goods_id)->setField('goods_del', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}